<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="物资名称" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="物资图" prop="image">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="dataForm.image" :src="dataForm.image" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="总量" prop="stock">
        <el-input v-model="dataForm.stock" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="剩余" prop="restStock">
        <el-input v-model="dataForm.restStock" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="每人限领" prop="eachQuantity">
        <el-input v-model="dataForm.eachQuantity" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
            uploadUrl: window.SITE_CONFIG.baseUrl + '/covid/file/upload',

      visible: false,
      dataForm: {
        id: 0,
        name: '',
        image: '',
        stock: '',
        restStock: '',
        eachQuantity: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        image: [{ required: true, message: '不能为空', trigger: 'blur' }],
        stock: [{ required: true, message: '不能为空', trigger: 'blur' }],
        restStock: [{ required: true, message: '不能为空', trigger: 'blur' }],
        eachQuantity: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 头像上传
    handleAvatarSuccess (res, file) {
      console.log(res.msg)
      // this.dataForm.image = URL.createObjectURL(file.raw)
      this.dataForm.image = res.msg
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/covid/supply/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name
              this.dataForm.image = data.supply.image
              this.dataForm.stock = data.supply.stock
              this.dataForm.restStock = data.supply.restStock
              this.dataForm.eachQuantity = data.supply.eachQuantity
              this.dataForm.createTime = data.supply.createTime
              this.dataForm.updateTime = data.supply.updateTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/supply/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              image: this.dataForm.image,
              stock: this.dataForm.stock,
              restStock: this.dataForm.restStock,
              eachQuantity: this.dataForm.eachQuantity,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
